#pragma once
#include<iostream>
#include<string>
#include"TcpServer.hpp"
#include"HttpProtocol.hpp"


class httpserver{
public:
    httpserver(int port) :tsvr(std::make_unique<tcpserver>(port)){
        
    }

    bool start(){
        tsvr->sethandle([this](SockPtr& sockfd,inetaddr& client){
            this->Handlerequest(sockfd,client);
        });
        tsvr->loop();
        return true;
    }
    bool Handlerequest(SockPtr& sockfd,inetaddr& client){
        
        std:: string http_request;
        sockfd->RecvData(&http_request);    
        std::string http_responds;

        HttpRequest request;
        request.prase(http_request);

        HttpRespond respond;
        respond.bulid(request);
        respond.serialization(&http_responds);

        sockfd->SendData(http_responds);
        sleep(1);
        return true;
    }
    ~httpserver(){
    }
private:
    std::unique_ptr<tcpserver> tsvr;
};